<template>
  <div class="client-management-container">
    <!-- 操作区域卡片 -->
    <el-card class="operation-card" shadow="hover">
      <div class="operation-area">
        <!-- 搜索区域 -->
        <div class="search-area">
          <el-input
              placeholder="仓库编号"
              v-model="searchWnumber"
              clearable
              prefix-icon="el-icon-user"
              class="search-input"
          ></el-input>

          <el-input
              placeholder="区域编号"
              v-model="searchRcode"
              clearable
              prefix-icon="el-icon-office-building"
              class="search-input"
          ></el-input>

          <el-input
              placeholder="类型位置"
              v-model="searchRlocation"
              clearable
              prefix-icon="el-icon-mobile-phone"
              class="search-input"
          ></el-input>

          <el-input
              placeholder="货物类型"
              v-model="searchGtype"
              clearable
              prefix-icon="el-icon-mobile-phone"
              class="search-input"
          ></el-input>

          <el-input
              placeholder="货物批次"
              v-model="searchGbatch"
              clearable
              prefix-icon="el-icon-mobile-phone"
              class="search-input"
          ></el-input>

          <el-input
              placeholder="入库时间"
              v-model="searchTime"
              clearable
              prefix-icon="el-icon-mobile-phone"
              class="search-input"
          ></el-input>

          <el-button
              type="primary"
              icon="el-icon-search"
              @click="handleSearch"
              class="search-button"
          >
            搜索
          </el-button>

          <el-button
              type="info"
              icon="el-icon-refresh"
              @click="handleReset"
              class="search-button"
          >
            重置
          </el-button>

          <el-button
              type="success"
              icon="el-icon-download"
              @click="handleExport"
              class="search-button"
              :disabled="selectedRowKeys.length === 0 && tableData.length === 0"
          >
            导出
          </el-button>
        </div>
      </div>
    </el-card>

    <!-- 数据表格区域 -->
    <el-card class="data-card" shadow="hover">
      <el-table
          :data="tableData"
          style="width: 100%"
          v-loading="loading"
          class="data-table"
          :header-cell-style="{ background: '#f5f7fa', color: '#606266' }"
          @selection-change="handleSelectionChange"
      >
        <el-table-column
            type="selection"
            width="55"
            align="center"
        ></el-table-column>

        <el-table-column
            type="index"
            label="序号"
            width="70"
            align="center"
            :index="calculateIndex"
        ></el-table-column>

        <el-table-column
            prop="wnumber"
            label="仓库编号"
            width="150"
            align="center"
        ></el-table-column>

        <el-table-column
            prop="rcode"
            label="区域编号"
            width="150"
            align="center"
        ></el-table-column>

        <el-table-column
            prop="rlocation"
            label="仓位位置"
            width="140"
            align="center"
        ></el-table-column>

        <el-table-column
            prop="gbatch"
            label="货物批次"
            width="140"
            align="center"
        ></el-table-column>

        <el-table-column
            prop="gtype"
            label="货物类型"
            width="140"
            align="center"
        ></el-table-column>

        <el-table-column
            prop="gnum"
            label="入库货物数量"
            width="80"
            align="center"
        ></el-table-column>

        <el-table-column
            prop="time"
            label="入库时间"
            width="140"
            align="center"
        ></el-table-column>

        <el-table-column
            prop="client.clientName"
            label="客户名称"
            width="120"
            align="center"
        ></el-table-column>

        <el-table-column
            prop="client.enterprise"
            label="所属公司"
            width="120"
            align="center"
        ></el-table-column>

        <el-table-column
            prop="client.clientPhone"
            label="联系方式"
            width="140"
            align="center"
        ></el-table-column>

      </el-table>

      <!-- 分页组件 -->
      <div class="pagination-container">
        <el-pagination
            background
            layout="total, sizes, prev, pager, next, jumper"
            :current-page="currentPage"
            :page-sizes="[5, 10, 15, 20]"
            :page-size="pageSize"
            :total="total"
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
        ></el-pagination>
      </div>
    </el-card>
  </div>
</template>

<script>
import request from '@/utils/request';

export default {
  data() {
    return {
      tableData: [],
      searchWnumber: '',
      searchRcode: '',
      searchRlocation: '',
      searchGtype: '',
      searchGbatch: '',
      searchTime: '',
      currentPage: 1,
      pageSize: 10,
      total: 0,
      loading: false,
      selectedRowKeys: [],
      selectedRows: [],
    }
  },
  mounted() {
    this.fetchUserList();
  },
  methods: {
    // 计算表格序号
    calculateIndex(index) {
      return (this.currentPage - 1) * this.pageSize + index + 1;
    },
    // 搜索记录
    handleSearch() {
      this.currentPage = 1;
      this.fetchUserList();
    },
    // 切换每页数量
    handleSizeChange(newSize) {
      this.pageSize = newSize;
      this.fetchUserList();
    },
    // 切换页码
    handleCurrentChange(newPage) {
      this.currentPage = newPage;
      this.fetchUserList();
    },
    // 获取入库记录列表
    fetchUserList() {
      this.loading = true;
      request.get('/inOut/getInWarehouseRecord', {
        params: {
          page: this.currentPage,
          size: this.pageSize,
          wnumber: this.searchWnumber,
          rcode: this.searchRcode,
          rlocation: this.searchRlocation,
          gtype: this.searchGtype,
          gbatch: this.searchGbatch,
          time: this.searchTime,
        }
      })
          .then(response => {
            this.tableData = response.data.data.list.list;
            this.total = response.data.data.list.total;
            this.loading = false;
          })
          .catch(error => {
            console.error('获取入库记录列表失败:', error);
            this.$message.error('获取入库记录列表失败');
            this.loading = false;
          });
    },
    // 重置搜索条件
    handleReset() {
      this.searchWnumber = '';
      this.searchRcode = '';
      this.searchRlocation = '';
      this.searchGtype = '';
      this.searchGbatch = '';
      this.searchTime = '';
      this.fetchUserList();
    },
    // 处理表格行选择
    handleSelectionChange(selection) {
      this.selectedRows = selection;
      this.selectedRowKeys = selection.map(row => row.id);
    },
    // 处理导出
    handleExport() {
      if (!this.hasExportPermission()) {
        this.$message.warning('你没有导出权限');
        return;
      }

      if (this.selectedRowKeys.length > 0) {
        this.exportSelectedRecords();
      } else if (this.tableData.length > 0) {
        this.$confirm('确定要导出当前搜索条件下的所有记录吗？', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          this.exportAllRecords();
        }).catch(() => {});
      } else {
        this.$message.warning('没有可导出的数据');
      }
    },
    // 导出选中记录
    exportSelectedRecords() {
      this.loading = true;
      request.post('/inOut/exportIntWarehouseRecord', {
        ids: this.selectedRowKeys,
      }, {
        responseType: 'blob',
        headers: {
          'Content-Type': 'application/json'
        }
      })
          .then(response => {
            this.downloadFile(response.data, '入库记录导出.xlsx');
            this.loading = false;
          })
          .catch(error => {
            console.error('导出失败:', error);
            this.$message.error('导出失败，请重试');
            this.loading = false;
          });
    },
    // 导出全部记录
    exportAllRecords() {
      this.loading = true;
      request.post('/inOut/exportIntWarehouseRecord', {
        wnumber: this.searchWnumber,
        rcode: this.searchRcode,
        rlocation: this.searchRlocation,
        gtype: this.searchGtype,
        gbatch: this.searchGbatch,
        time: this.searchTime,
        exportAll: true,
      }, {
        responseType: 'blob',
        headers: {
          'Content-Type': 'application/json'
        }
      })
          .then(response => {
            this.downloadFile(response.data, '入库记录导出.xlsx');
            this.loading = false;
          })
          .catch(error => {
            console.error('导出失败:', error);
            this.$message.error('导出失败，请重试');
            this.loading = false;
          });
    },
    // 下载文件
    downloadFile(blobData, fileName) {
      const blob = new Blob([blobData], { type: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet' });
      const url = window.URL.createObjectURL(blob);
      const a = document.createElement('a');
      a.href = url;
      a.download = fileName;
      document.body.appendChild(a);
      a.click();
      setTimeout(() => {
        document.body.removeChild(a);
        window.URL.revokeObjectURL(url);
      }, 0);
    },
    // 权限检查
    hasExportPermission() {
      return true; // 实际项目中替换为真实权限判断
    }
  }
}
</script>

<style scoped>
.client-management-container {
  padding: 20px;
  background-color: #f0f2f5;
  min-height: calc(100vh - 80px);
}

.operation-card {
  margin-bottom: 20px;
  border-radius: 8px;
  border: none;
  background-color: rgba(255, 255, 255, 0.9);
  box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
}

.operation -buttons {
  display: flex;
  justify-content: space-between;
  margin-bottom: 5px;
}

.data-card {
  border-radius: 8px;
  border: none;
  background-color: rgba(255, 255, 255, 0.9);
  box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
}

.operation-area {
  display: flex;
  justify-content: space-between;
  align-items: center;
  flex-wrap: wrap;
}

.search-area {
  display: flex;
  align-items: center;
  flex-wrap: wrap;
  gap: 10px;
}

.search-input {
  width: 180px;
}

.search-button {
  margin-left: 10px;
}

.search-button {
  margin-left: 10px;
  transition: all 0.3s;
  border-radius: 4px;
}

.search-button:hover {
  transform: translateY(-2px);
  box-shadow: 0 4px 12px rgba(58, 123, 213, 0.2);
}

.action-button {
  transition: all 0.3s;
  border-radius: 4px;
}

.action-button:hover {
  transform: translateY(-2px);
  box-shadow: 0 4px 12px rgba(58, 123, 213, 0.2);
}

.data-table {
  margin-top: 10px;
}

.pagination-container {
  margin-top: 20px;
  display: flex;
  justify-content: flex-end;
}

/deep/ .tech-dialog {
  border-radius: 8px;
  background: linear-gradient(145deg, #f5f7fa, #e4e7ed);
  border: 1px solid rgba(58, 123, 213, 0.2);
  box-shadow: 0 10px 30px rgba(0, 0, 0, 0.2);
}

/deep/ .tech-dialog .el-dialog__header {
  background: linear-gradient(90deg, #3a7bd5, #00d2ff);
  border-radius: 8px 8px 0 0;
  padding: 15px 20px;
}

/deep/ .tech-dialog .el-dialog__title {
  color: white;
  font-weight: 600;
}

/deep/ .tech-dialog .el-dialog__headerbtn .el-dialog__close {
  color: white;
}

.tech-form {
  padding: 20px;
}

/deep/ .tech-form .el-input__inner {
  background-color: rgba(255, 255, 255, 0.8);
  border: 1px solid #dcdfe6;
  border-radius: 4px;
  transition: all 0.3s;
}

/deep/ .tech-form .el-input__inner:hover {
  border-color: #c0c4cc;
}

/deep/ .tech-form .el-input__inner:focus {
  border-color: #3a7bd5;
  box-shadow: 0 0 0 2px rgba(58, 123, 213, 0.2);
}

.disabled-input /deep/ .el-input__inner {
  background-color: #f5f7fa;
  color: #909399;
  cursor: not-allowed;
}

.cancel-button {
  background-color: #f5f7fa;
  border-color: #dcdfe6;
  color: #606266;
  transition: all 0.3s;
}

.cancel-button:hover {
  background-color: #e4e7ed;
  border-color: #c0c4cc;
  color: #606266;
}

.confirm-button {
  background: linear-gradient(90deg, #3a7bd5, #00d2ff);
  border: none;
  transition: all 0.3s;
}

.confirm-button:hover {
  background: linear-gradient(90deg, #2c5fb3, #00b7e6);
  transform: translateY(-2px);
  box-shadow: 0 4px 12px rgba(58, 123, 213, 0.3);
}

/deep/ .tech-message-box {
  border-radius: 8px;
  background: linear-gradient(145deg, #f5f7fa, #e4e7ed);
  border: 1px solid rgba(58, 123, 213, 0.2);
  box-shadow: 0 10px 30px rgba(0, 0, 0, 0.2);
}

/deep/ .tech-message-box .el-message-box__header {
  background: linear-gradient(90deg, #3a7bd5, #00d2ff);
  border-radius: 8px 8px 0 0;
  padding: 15px 20px;
}

/deep/ .tech-message-box .el-message-box__title {
  color: white;
  font-weight: 600;
}

/deep/ .tech-message-box .el-message-box__content {
  padding: 20px;
}

/deep/ .tech-message-box .el-message-box__btns {
  padding: 10px 20px 20px;
}
</style>